/**
 * 
 */
package com.obradovicnjegovan.webprodavnica.pages;

import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.tynamo.security.services.SecurityService;

import com.obradovicnjegovan.webprodavnica.controller.ControllerAL;
import com.obradovicnjegovan.webprodavnica.entities.Korisnik;
import com.obradovicnjegovan.webprodavnica.entities.TipKorisnika;

/**
 * @author njegovan
 *
 */
public class Register {
	@Inject
	private ControllerAL controllerAL;
	@Property
	private Korisnik korisnik;
	@Inject
	private SecurityService securityService;
	
	@CommitAfter
	public Object onSuccess(){
		try {
			korisnik.getRoles().add(TipKorisnika.kupac);
			controllerAL.registrujKorisnika(korisnik);
			UsernamePasswordToken token = new UsernamePasswordToken(korisnik.getUsername(), korisnik.getPassword());
			Subject subject = securityService.getSubject();
			subject.login(token);
			return Index.class;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
}